<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="iOS开发自定义NSLog"><meta name="keywords" content="iOS"><meta name="author" content="kyren"><meta name="copyright" content="kyren"><title>iOS开发自定义NSLog | Kyren's Blog</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cf87fcdbd9ab7cf93d894f7c219ae78f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script src="https://v1.hitokoto.cn/?encode=js&amp;charset=utf-8&amp;select=.footer_custom_text" defer></script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '6.2.0'
} </script><meta name="generator" content="Hexo 6.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B1%8F%E8%94%BDXcode%E7%83%A6%E4%BA%BALog"><span class="toc-number">1.</span> <span class="toc-text">屏蔽Xcode烦人Log</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#C%E8%AF%AD%E8%A8%80%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="toc-number">2.</span> <span class="toc-text">C语言宏定义</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Xcode8%E5%90%8E%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89Log"><span class="toc-number">3.</span> <span class="toc-text">Xcode8后的自定义Log</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%AE%8F"><span class="toc-number">4.</span> <span class="toc-text">格式化宏</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8E%A7%E5%88%B6Log%E8%BE%93%E5%87%BA"><span class="toc-number">5.</span> <span class="toc-text">控制Log输出</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://avatars.githubusercontent.com/u/5827823?v=4"></div><div class="author-info__name text-center">kyren</div><div class="author-info__description text-center">huluo的技术博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://github.com/huluo666">Follow Me</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">239</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">41</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">29</span></a></div><hr><div class="author-info-links"><div class="author-info-links__title text-center">Links</div><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:9090/">Jira</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:8090/">Confluence</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://bloc.leniugame.com/">Leniu</a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">Kyren's Blog</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">主页</a><a class="site-page" href="/essay">随笔</a><a class="site-page" href="/archives">归档</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a><a class="site-page" href="/about">关于</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">iOS开发自定义NSLog</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2017-03-22</time></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h5 id="屏蔽Xcode烦人Log"><a href="#屏蔽Xcode烦人Log" class="headerlink" title="屏蔽Xcode烦人Log"></a>屏蔽Xcode烦人Log</h5><p>Xcode8之后，会打印一些烦人的Log信息</p>
<p>解决方法：1- Xcode菜单按钮: <code>Product &gt; Scheme &gt; Edit Scheme</code>&gt;<code>Run</code>&gt;<code>Arguments</code>或者直接按 <code>command + shift + &lt;</code> 快捷键</p>
<p>​		   2- <strong>Environment Variables</strong> 设置为 <code>OS_ACTIVITY_MODE</code> &#x3D; <code>disable</code></p>
<p>stackoverflow:<a target="_blank" rel="noopener" href="http://stackoverflow.com/questions/37800790/hide-strange-unwanted-xcode-8-logs">hide-strange-unwanted-xcode-8-logs</a></p>
<h5 id="C语言宏定义"><a href="#C语言宏定义" class="headerlink" title="C语言宏定义"></a>C语言宏定义</h5><table>
<thead>
<tr>
<th>符号</th>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><code>__FILE__</code></td>
<td>String</td>
<td>当前文件的绝对路径</td>
</tr>
<tr>
<td><code>__LINE__</code></td>
<td>Int</td>
<td>展开该宏时在文件中的行数</td>
</tr>
<tr>
<td><code>__COLUMN__</code></td>
<td>Int</td>
<td>展开该宏时在文件中的列</td>
</tr>
<tr>
<td><code>__FUNCTION__</code></td>
<td>String</td>
<td>包含这个符号的函数名称</td>
</tr>
<tr>
<td><code>__COUNTER__</code></td>
<td>Int</td>
<td>无重复的计数器，从程序启动开始每次调用都会++，常用语宏中定义无重复的参数名称</td>
</tr>
</tbody></table>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NSLog(@&quot;%s %d %s %s&quot;, __FILE__, __LINE__, __PRETTY_FUNCTION__, __FUNCTION__);</span><br></pre></td></tr></table></figure>



<p><code>printf()</code>和<code>fprintf()</code>这些输出函数的参数是可变的</p>
<p>C99中规定宏可以像函数一样带有可变参数：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#define LOG(format, ...) fprintf(stdout, format, __VA_ARGS__)</span><br></pre></td></tr></table></figure>

<p>其中，<code>...</code>表示参数可变，<code>__VA_ARGS__</code>在预处理中为实际的参数集所替换</p>
<p>GCC中同时支持如下的形式</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#define LOG(format, args...) fprintf(stdout, format, args)</span><br></pre></td></tr></table></figure>

<p>其用法和上面的基本一致，只是参数符号有变化</p>
<p>“<code>##</code>“的作用是对token进行连接，在上例中，<code>format</code>、<code>__VA_ARGS__</code>、<code>args</code>即是token，</p>
<p>如果token为空，那么不进行连接，所以允许省略可变参数(__VA_ARGS__和args)，对上述变参宏做如下修改</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#define LOG(format, ...)     fprintf(stdout, format, ##__VA_ARGS__)</span><br><span class="line">#define LOG(format, args...) fprintf(stdout, format, ##args)</span><br></pre></td></tr></table></figure>

<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//关键字</span></span><br><span class="line">...:可变参数</span><br><span class="line"> __VA_ARGS__ :宏定义中的...中的所有剩余参数</span><br><span class="line"> ##:连接符号</span><br><span class="line"> #:原样输出</span><br><span class="line">/:换行符</span><br></pre></td></tr></table></figure>



<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> K_AAAAA(aaa,...) aaa</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> K_BBBBB(...) K_AAAAA(__VA_ARGS__)</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//调用示例：</span></span><br><span class="line"> <span class="built_in">NSLog</span>(<span class="string">@&quot;outPut :%@&quot;</span>,K_BBBBB(<span class="string">@&quot;this is string&quot;</span>));</span><br><span class="line"> <span class="built_in">NSLog</span>(<span class="string">@&quot;outPut2 :%d&quot;</span>,K_BBBBB(<span class="number">100</span>));</span><br><span class="line"> <span class="comment">//输出： outPut :this is string</span></span><br><span class="line">        outPut2 :<span class="variable language_">this</span> is <span class="number">100</span></span><br></pre></td></tr></table></figure>



<h5 id="Xcode8后的自定义Log"><a href="#Xcode8后的自定义Log" class="headerlink" title="Xcode8后的自定义Log"></a>Xcode8后的自定义Log</h5><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> NNSLog00(format,...) NSLog(format,__VA_ARGS__);</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NNSLog0(fmt, ...) NSLog((fmt), ##__VA_ARGS__);</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NNSLog(...) fprintf(stderr,<span class="string">&quot;%s\n&quot;</span>,[[NSString stringWithFormat:__VA_ARGS__] UTF8String]);</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NNSLog1(FORMAT,...) fprintf(stderr,<span class="string">&quot;%s\n&quot;</span>,[[NSString stringWithFormat:FORMAT,##__VA_ARGS__] UTF8String]);</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NNSLog2(...) fprintf(<span class="string">&quot;%s %s Line %d: %s\n&quot;</span>,[[NSString stringWithFormat:@<span class="string">&quot;%s&quot;</span>, __FILE__].lastPathComponent UTF8String], __PRETTY_FUNCTION__, __LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);</span></span><br></pre></td></tr></table></figure>



<h5 id="格式化宏"><a href="#格式化宏" class="headerlink" title="格式化宏"></a>格式化宏</h5><p>可以&#x2F;换行符格式化宏，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#define NSLog(format, ...) \</span><br><span class="line">    do &#123; \</span><br><span class="line">        NSLog(@&quot;&lt;%@ : %d : %s&gt;-: %@&quot;, \</span><br><span class="line">        [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \</span><br><span class="line">        __LINE__, \</span><br><span class="line">        __FUNCTION__, \</span><br><span class="line">        [NSString stringWithFormat:format, ##__VA_ARGS__]); \</span><br><span class="line">    &#125; while(0)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h5 id="控制Log输出"><a href="#控制Log输出" class="headerlink" title="控制Log输出"></a>控制Log输出</h5><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifdef</span> DEBUG</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NSLog(fmt, ...) NSLog((fmt), ##__VA_ARGS__);</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NSLog(...) &#123;&#125;</span></span><br><span class="line"><span class="comment">//#define NSLog(format, ...) do&#123; &#125; while(0)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>



<p>如果在项目中log信息太多，可以屏蔽共用log，使用自定义log，便于局部测试</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#define NSLog(...) &#123;&#125;//屏蔽共用log</span><br><span class="line">#define NNSLog(...) fprintf(stderr,&quot;%s\n&quot;,[[NSString stringWithFormat:__VA_ARGS__] UTF8String]);//使用自定义log</span><br></pre></td></tr></table></figure>

<p>参考</p>
<p><a target="_blank" rel="noopener" href="https://onevcat.com/2014/01/black-magic-in-macro/">https://onevcat.com/2014/01/black-magic-in-macro/</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">kyren</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://huluo666.gitee.io/2017/03/22/iOS自定义Log/">http://huluo666.gitee.io/2017/03/22/iOS自定义Log/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://huluo666.gitee.io">Kyren's Blog</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/iOS/">iOS</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2017/03/28/Swift%E8%BD%AC%E6%8D%A2OBJECTIVE%20C/"><i class="fa fa-chevron-left">  </i><span>OC与Swift代码转换工具</span></a></div><div class="next-post pull-right"><a href="/2017/03/20/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%BD%BF%E7%94%A8/"><span>正则表达式使用</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2022 By kyren</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="footer_custom_text">hitokoto</div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/lib/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>